package com.example.todofinally.adaoter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.todofinally.R;
import com.example.todofinally.edit_Activity;
import com.example.todofinally.sql.Data;
import com.example.todofinally.sql.SpHelper;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

public class ListAdapter extends  RecyclerView.Adapter<ListAdapter.LinearViewHolder>{

    private Context mContext;
    private List<Data> mvalue;
    public ListAdapter(Context context, List<Data> list){
        this.mvalue=list;
        this.mContext = context;
    }

    @NonNull
    @Override
    public LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_list,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull LinearViewHolder holder, @SuppressLint("RecyclerView") int position) {//绑定数据
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             SpHelper mHelper=new SpHelper(mContext);
                String id = String.valueOf(mvalue.get(position).id);
              mHelper.delete(id);
                mvalue.remove(position);

              notifyDataSetChanged();


            }
        });

if(mvalue.get(position).status==1)
{
    holder.checkBox.setChecked(true);
}
        holder.input_time.setText(mvalue.get(position).startTime);
        holder.input_date.setText(mvalue.get(position).date);
        holder.input_content.setText(mvalue.get(position).editContent);
        InputStream inputStream = null;
          Uri  imageUri = Uri.parse(mvalue.get(position).photo);
        try {
            inputStream =mContext.getContentResolver().openInputStream(imageUri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            holder.img.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SpHelper mHelper=new SpHelper(mContext);
                if(!holder.checkBox.isChecked())
                {
                    Log.d("MainActivity", "onClick: ");
                    mHelper.changeUnfinished(mvalue.get(position).id+"");
                    Toast.makeText(mContext, "取消完成事件", Toast.LENGTH_SHORT).show();

                }
                else mHelper.changeFinished(mvalue.get(position).id+"");
                        Toast.makeText(mContext, "该事件已完成", Toast.LENGTH_SHORT).show();

                    }
                });
        holder.input_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(holder.img.getVisibility() == View.GONE){
                    holder.img.setVisibility(View.VISIBLE);
                }else {
                    holder.img.setVisibility(View.GONE);
                }
//                Toast.makeText(mContext, "111111", Toast.LENGTH_SHORT).show();
            }
        });

        holder.input_content.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Intent intent = new Intent(mContext, edit_Activity.class);
                Log.d("获取适配器", "onLongClick: "+mvalue.get(position).id);
                intent.putExtra("id",mvalue.get(position).id+"");
                mContext.startActivity(intent);
                return false;
            }
        });



        Data data = mvalue.get(position);
        if(data.getStatus() == 2){
            holder.background.setBackgroundColor(Color.parseColor("#FF0000"));
            holder.checkBox.setClickable(false);
            holder.input_content.setClickable(false);
        }

        Log.d("搜索", "onBindViewHolder: " +mvalue.get(position).date);
        // holder.end_time.setText(mvalue.get(position).id);
    }

    @Override
    public int getItemCount() {
        return mvalue.size();
    }

    class LinearViewHolder extends RecyclerView.ViewHolder {//绑定控件


        private TextView input_title,input_time,input_date,input_content;
        private ImageButton delete;
        private ImageView img;
        private CheckBox checkBox;
        private LinearLayout background;


        public LinearViewHolder(@NonNull View itemView) {
            super(itemView);
            checkBox=itemView.findViewById(R.id.checkbox);
            delete = itemView.findViewById(R.id.delete);
            input_content = itemView.findViewById(R.id.input_content);
            input_time = itemView.findViewById(R.id.input_time);
            input_date = itemView.findViewById(R.id.input_date);
            img = itemView.findViewById(R.id.img);
            background = itemView.findViewById(R.id.background);
           /* point = itemView.findViewById(R.id.point);
            event_time = itemView.findViewById(R.id.event_time);*/
        }
    }
}

